package com.zero.rocketmq.api.base.producer.sync;

import com.zero.rocketmq.api.utils.RocketMQContents;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.common.RemotingHelper;

/**
 * 同步发送消息
 */
public class SyncProducer {

    public static void main(String[] args) throws MQClientException {

        DefaultMQProducer producer = new DefaultMQProducer(RocketMQContents.PRODUCER_GROUP);
        producer.setNamesrvAddr(RocketMQContents.NAMESERVER_ADDR);
        producer.start();

        try {
            for (int i = 0; i < 10; i++) {

                Message message = new Message();
                message.setTopic("TopicTest");
                message.setTags("TagA");
                message.setKeys("OrderID188");
                message.setBody("Hello world".getBytes(RemotingHelper.DEFAULT_CHARSET));
                //同步传递消息，消息会发给集群中的一个Broker节点
                SendResult sendResult = producer.send(message,10000);
                System.out.println("Producer send message result:"+sendResult);
            }
        }catch (Exception e){
            e.printStackTrace();
        }

        producer.shutdown();
    }
}
